<?php

namespace common\behavior\CommonMessagePushing;


use common\model\EntityModel;
use common\model\ProjectMemberModel;
use common\model\TaskModel;
use common\model\UserModel;
use common\observer\RelationChangeAutoAssembleCheck;
use common\service\BehaviorService;
use common\service\EntityService;
use common\service\OptionsService;
use common\service\TaskService;
use common\service\UserService;
use Webman\Stomp\Client;

/**
 * 实体属性变更 消息通知
 */
class EntityAttributeChangeBehavior
{
    /**
     * @param $params
     * @return void
     */
    public function run($params)
    {
        /***********  预处理开始  ************/
        $params = BehaviorService::getInstance()->dealParam(
            $params,
            [
                'entity' => '',
                'entity_module_code' => 'strval',
                'note_text' => 'strval|trim',
            ],
            [
                'entity' => [],
                'entity_module_code' => '',
                'note_text' => '',
            ],
            'data',
            ['type' => 'entity_attribute_change'],
        );
        /***********  行为判断  ************/
        if ($params === false) {
            return;
        }
        /***********  变量迁移  ************/
        list($entity, $entityModuleCode, $noteText) = $params;
        if (empty($entity) || empty($entityModuleCode)) {
            return;
        }
        /***********  预处理结束  ************/

        /***********  原逻辑迁移  ************/

        // 查询相关人员
        $relatedPeoples = EntityService::getInstance()->getEntityRelatePeople($entity, false);
        // 通知相关人员
        if (in_array($entityModuleCode, ['asset', 'level', 'design'])) {
            // 资产线 查找概念
            if ($entityModuleCode === 'design') {
                $upstreamEntity = $entity;
            } else {
                $upstreamEntity = EntityService::getInstance()->getUpstreamEntity($entity);
            }
        } else {
            // 镜头线
            if ($entityModuleCode === 'storyboard') {
                $upstreamEntity = $entity;
            } else {
                $upstreamEntity = EntityService::getInstance()->getUpstreamEntity($entity);
            }
        }

        $templateId = OptionsService::getInstance()->getEntityTaskRemindCommonTemplateId();
        $notifyParam = [
            'first' => '任务信息变更',
            'note_text' => $noteText,
            'remark' => "请及时查看",
            'tenant_id' => $upstreamEntity['tenant_id'],
            'project_id' => $upstreamEntity['project_id'],
            'entity_id' => $upstreamEntity['id'],
            'module_code' => in_array(module_by_id($upstreamEntity['module_id'])['code'], ['design', 'level', 'asset']) ? 'design' : 'storyboard',
            'message_key' => 'entity_attribute_change',
        ];
        $messageGenerated = [];
        foreach ($relatedPeoples as $people) {
            $messageGenerated[] = [
                "param" => $notifyParam,
                'receiver' => $people['union_id'],
                'template_id' => $templateId,
                'content_type' => 'card_message',
                'notice_type' => 'message_demand',
                'is_popup' => 1,
                'message_key' => 'entity_attribute_change',
            ];
        }

        /***********  发送新的队列  ************/
        foreach ($messageGenerated as $message) {
            Client::send('common_message_pushing', build_queue_data_with_xu([
                'message_generated' => [$message],
            ]));
        }
        /***********  结束  ************/
    }
}
